import com.mochimedia.ui.*;

class com.mochimedia.ui.Create {

    public static var ICON_ARROW_LEFT:Array = [ -0.2, -0.5, 0.4, 0, -0.2, 0.5];
    public static var ICON_ARROW_RIGHT:Array = [ 0.2, -0.5, -0.4, 0, 0.2, 0.5];
    public static var ICON_ARROW_UP:Array = [ -0.5, 0.3, 0, -0.3, 0.5, 0.3];
    public static var ICON_ARROW_DOWN:Array = [ -0.5, -0.3, 0, 0.3, 0.5, -0.3];
	public static var ICON_CHECK:Array = [0.5, -0.2, -0.1, 0.4, -0.5, 0, -0.3, -0.2, -0.1, 0, 0.3, -0.4, 0.5, -0.2];
 	public static var ICON_PLUS:Array = [0.12, -0.5, 0.12, -0.12, 0.5, -0.12, 0.5, 0.12, 0.12, 0.12, 0.12, 0.5, -0.12, 0.5, -0.12, -0.12, -0.5, -0.12, -0.5, 0.12, -0.12, 0.12, -0.12, -0.5];
	public static var ICON_MINUS:Array = [ -0.5, -0.12, 0.5, -0.12, 0.5, 0.12, -0.5, 0.12];
 	
	public static var ICON_PLAY:Array = [ -0.3, -0.5, 0.5, 0, -0.3, 0.5];
	public static var ICON_PAUSE:Array = [-0.1, -0.5, -0.1, 0.5, -0.5, 0.5, -0.5, -0.5, -0.1, -0.5, 0.5, -0.5, 0.5, 0.5, 0.1, 0.5, 0.1, -0.5, 0.5, -0.5];
	public static var ICON_CLOSE:Array = [0.5, -0.5, 0.5, -0.31, 0.18, 0, 0.345, 0.165, 0.35, 0.165, 0.5, 0.315, 0.5, 0.5, 0.31, 0.5, 0, 0.18, -0.095, 0.28, -0.095, 0.28, -0.315, 0.5, -0.5, 0.5, -0.5, 0.315, -0.18, 0, -0.28, -0.095, -0.28, -0.095, -0.5, -0.31, -0.5, -0.5, -0.315, -0.5, -0.145, -0.325, -0.145, -0.325, 0, -0.18, 0.31, -0.5, 0.5, -0.5];
		    
    //
    //
    public static function button (mc:MovieClip, textLabel:String, textAlign:Number, width:Number, height:Number, tabMode:Boolean, groupMode:Boolean, style:Style):MovieClip {
        
        if (style == undefined) style = new Style();
        
        var cornerRoundness:String = String(style.round);
        if (tabMode) cornerRoundness = cornerRoundness + " " + cornerRoundness + " 0 0";

        var button:MovieClip
        var button_tf:TextField;
        var button_btn:MovieClip;
        var button_inactive:MovieClip;
        var button_tf_inactive:TextField;

        var g:MovieClip = mc;

        var temp:Array = cornerRoundness.split(" ");
        for (var i:Number = 0; i < temp.length; i++) temp[i] = (parseInt(temp[i]) > 0) ? parseInt(temp[i]) - style.borderWidth : 0;
        var innerRoundness:String = temp.join(" ");

        button = mc.createEmptyMovieClip("_button" + mc.getNextHighestDepth() + 1, mc.getNextHighestDepth());
    
        button_tf = newText(textLabel, "_buttontext", button, style, (groupMode) ? style.unselectedTextColor : style.inverseTextColor, style.fontSize);
    
        if (isNaN(width)) width = button_tf._width + style.padding * 3 + style.borderWidth * 2;
        else width = Math.max(width, button_tf._width + style.padding * 3 + style.borderWidth * 2 - 2);
        
        if (isNaN(height)) height = button_tf._height + style.padding + style.borderWidth * 2;
        else height = Math.max(height, button_tf._height + style.padding + style.borderWidth * 2 - 2);

        if (textAlign == Position.ALIGN_LEFT) button_tf._x = style.padding * 3;
        else if (textAlign == Position.ALIGN_RIGHT) button_tf._x = width - button_tf._width - style.padding * 3;   
        else button_tf._x = Math.floor((width - button_tf._width) * 0.5);
        
        button_tf._y = Math.floor((height - button_tf._height) * 0.5);
        
        g = button;
        
        var vpad:Number = 0;
        if (tabMode) vpad = style.borderWidth;

        if (style.border) DrawingMethods.roundedRect(g, false, 0, 0, width, height - vpad, cornerRoundness, [style.borderColor], [style.backgroundAlpha], [100]);
        if (style.background) DrawingMethods.roundedRect(g, false, style.borderWidth, style.borderWidth, width - style.borderWidth * 2, height - style.borderWidth * 2, innerRoundness, [(groupMode) ? style.unselectedColor : style.buttonColor], [style.backgroundAlpha], [100]);
        if (style.gradient) DrawingMethods.roundedRect(g, false, style.borderWidth, style.borderWidth, width - style.borderWidth * 2, height - style.borderWidth * 2, innerRoundness, [0xffffff, 0xffffff, 0x000000, 0x000000], [40, 15, 0, 10], [0, 128, 128, 255]);

        button_btn = button.createEmptyMovieClip("btn", button.getNextHighestDepth());
        
        g = button_btn;
        DrawingMethods.roundedRect(g, false, style.borderWidth, style.borderWidth, width - style.borderWidth * 2, height - style.borderWidth * 2, innerRoundness, [0xffffff], [20], [100]);
        button_btn._alpha = 0;
        

        var button_selected:MovieClip;
        var button_tf_selected:TextField;
        
        button_selected = button.createEmptyMovieClip("btn_selected", button.getNextHighestDepth());
        g = button_selected;
        if (style.border) DrawingMethods.roundedRect(g, false, 0, 0, width, height - vpad, cornerRoundness, [style.inverseTextColor], [style.backgroundAlpha], [100]);
        if (style.background) DrawingMethods.roundedRect(g, false, style.borderWidth, style.borderWidth, width - style.borderWidth * 2, height - style.borderWidth * 2, innerRoundness, [style.buttonColor], [style.backgroundAlpha], [100]);
        if (style.gradient) DrawingMethods.roundedRect(g, false, style.borderWidth, style.borderWidth, width - style.borderWidth * 2, height - style.borderWidth * 2, innerRoundness, [0xffffff, 0xffffff, 0x000000, 0x000000], [40, 15, 0, 10], [0, 128, 128, 255]);
        button_tf_selected = newText(textLabel, "_buttontext", button_selected, style, style.inverseTextColor, style.fontSize);
        button_tf_selected._x = button_tf._x;
        button_tf_selected._y = button_tf._y;
        button_selected._visible = false;           


        button_inactive = button.createEmptyMovieClip("btn_inactive", button.getNextHighestDepth());
        g = button_inactive;
        if (style.border) DrawingMethods.roundedRect(g, false, style.borderWidth, style.borderWidth, width - style.borderWidth * 2, height - style.borderWidth * 2, innerRoundness, [style.inactiveColor], [style.backgroundAlpha], [100]);
        button_tf_inactive = newText(textLabel, "_buttontext", button_inactive, style, style.inactiveTextColor, style.fontSize);
        button_tf_inactive._x = button_tf._x;
        button_tf_inactive._y = button_tf._y;
        button_inactive._visible = false;
        
        return button;

    }
    
    //
    //
    public static function iconButton (mc:MovieClip, clip:Clip, textAlign:Number, width:Number, height:Number, tabMode:Boolean, groupMode:Boolean, style:Style):MovieClip {
        
        var btn:MovieClip = button(mc, "  " + clip.alt, Position.ALIGN_LEFT, width, height, tabMode, groupMode, style);
        var icn:MovieClip = btn.createEmptyMovieClip("_icon", btn.getNextHighestDepth());
        
        clip.width = clip.height = 16;
        clip.container = icn;
        clip.create();
        
        icn._x = 4 + style.borderWidth;
        icn._y = Math.floor(btn._height * 0.5) - 8 + style.borderWidth;
        icn._xscale = icn._yscale = 101;
        if (tabMode) icn._y -= 2;
        
        return btn;
        
    }
    
    //
    //
    public static function hitArea (mc:MovieClip, icon:Array, width:Number, height:Number, style:Style):MovieClip {
        
        if (style == undefined) style = new Style();
        
        var button:MovieClip
        var button_btn:MovieClip;
        var button_inactive:MovieClip;

        var g:MovieClip = mc;

        button = mc.createEmptyMovieClip("_button" + mc.getNextHighestDepth() + 1, mc.getNextHighestDepth());
    
        if (style.border) DrawingMethods.roundedRect(g, false, 0, 0, width, height, "0", [style.borderColor], [style.backgroundAlpha], [100]);
        if (style.background) DrawingMethods.roundedRect(g, false, style.borderWidth, style.borderWidth, width - style.borderWidth * 2, height - style.borderWidth * 2, "0", [style.unselectedColor], [style.backgroundAlpha], [100]);
        else DrawingMethods.rect(g, false, 0, 0, width, height, 0x000000, 0);
        if (style.gradient) DrawingMethods.roundedRect(g, false, style.borderWidth, style.borderWidth, width - style.borderWidth * 2, height - style.borderWidth * 2, "0", [0xffffff, 0xffffff, 0x000000, 0x000000], [40, 15, 0, 10], [0, 128, 128, 255]);
        if (icon != undefined) newIcon(icon, g, style.inverseTextColor, 100);
        
        button_btn = button.createEmptyMovieClip("btn", button.getNextHighestDepth());
        
        g = button_btn;
        DrawingMethods.roundedRect(g, false, style.borderWidth, style.borderWidth, width - style.borderWidth * 2, height - style.borderWidth * 2, "0", [0xffffff], [20], [100]);
        button_btn._alpha = 0;
    
        var button_selected:MovieClip;

        button_selected = button.createEmptyMovieClip("btn_selected", button.getNextHighestDepth());
        g = button_selected;
		DrawingMethods.rect(g, false, 0, 0, width, height, 0x000000, 0);
        if (style.border) DrawingMethods.roundedRect(g, false, 0, 0, width, height, "0", [style.borderColor], [style.backgroundAlpha], [100]);
        if (style.background) DrawingMethods.roundedRect(g, false, style.borderWidth, style.borderWidth, width - style.borderWidth * 2, height - style.borderWidth * 2, "0", [style.buttonColor], [style.backgroundAlpha], [100]);
        if (style.gradient) DrawingMethods.roundedRect(g, false, style.borderWidth, style.borderWidth, width - style.borderWidth * 2, height - style.borderWidth * 2, "0", [0xffffff, 0xffffff], [40, 0], [0, 255]);
        if (icon != undefined) newIcon(icon, g, style.inverseTextColor, 100);
        
        button_selected._visible = false;           

        button_inactive = button.createEmptyMovieClip("btn_inactive", button.getNextHighestDepth());
        g = button_inactive;
		DrawingMethods.rect(g, false, 0, 0, width, height, 0x000000, 0);
        if (style.border) DrawingMethods.roundedRect(g, false, style.borderWidth, style.borderWidth, width - style.borderWidth * 2, height - style.borderWidth * 2, "0", [style.inactiveColor], [style.backgroundAlpha], [100]);   
        if (icon != undefined) newIcon(icon, g, style.inactiveTextColor, 100);
        
        button_inactive._visible = false;
        
        return button;

    }
    
    //
    //
    public static function newText (text:String, id:String, container:MovieClip, style:Style, color:Number, size:Number, html:Boolean):TextField {
        
        if (isNaN(color)) color = style.textColor;
        if (isNaN(size)) size = style.fontSize;

        var tf:TextField;
        id = id + container.getNextHighestDepth();
        container.createTextField(id, container.getNextHighestDepth(), 0, 0, 200, 10);
        tf = container[id];
        tf.autoSize = "left";
        tf.embedFonts = style.embedFonts;
        
        if (html) {
            tf.html = true;
            tf.htmlText = text;
        } else {
            tf.text = text;
        }
        
        tf.selectable = false;
        tf.setTextFormat(new TextFormat(style.font, size, color, true ));
        tf.textColor = color;

        return tf;
    
    }
    
    //
    //
    public static function inputText (text:String, id:String, container:MovieClip, width:Number, height:Number, style:Style):TextField {
        
        var tf:TextField = newText(text, id, container, style);
        
        tf.selectable = true;
        tf.multiline = false;
        tf.wordWrap = false;
        
        tf.type = "input";
        
        tf.autoSize = true;
        
        tf.setTextFormat(new TextFormat("Verdana", style.fontSize, style.textColor, false, false, false, "", "", "left"));
        tf.setNewTextFormat(new TextFormat("Verdana", style.fontSize, style.textColor, false, false, false, "", "", "left"));
        
        tf.autoSize = false;
        
        if (!isNaN(width) && width > 0) {
            tf._width = width;
        }
        
        if (!isNaN(height) && height > 0) {
            tf._height = height;
        } else {
            tf._height = tf._height + 2 + style.borderWidth * 2;
        }
        
        return tf;
        
    }
    
    //
    //
    public static function newIcon (points:Array, mc:MovieClip, color:Number, alpha:Number):Void {
        
        var icon:MovieClip = mc.createEmptyMovieClip("_icon_" + mc.getNextHighestDepth(), mc.getNextHighestDepth());
        
        if (mc.icon == undefined) mc.icon = icon;
        
        icon._x = mc._width * 0.5;
        icon._y = mc._height * 0.5;
        
        var g:MovieClip = icon;
        
        if (points.length > 2) {
            
            g.beginFill(color, alpha);
            g.moveTo(points[0] * mc._width * 0.5, points[1] * mc._height * 0.5);
            
            for (var i:Number = 2; i < points.length; i+=2) {
                g.lineTo(points[i] * mc._width * 0.5, points[i + 1] * mc._height * 0.5);
            }
            
            g.endFill();
            
        }
        
    }
	
	//
	//
	public static function background (mc:MovieClip, width:Number, height:Number, style:Style, border:Boolean, round:Number):Void {
		
		if (style == undefined) style = Component.globalStyle;
		if (border == undefined) border = false;
		if (round == undefined) round = -1;
		
		var b:Number = (border) ? style.borderWidth : 0;
		var r:Number = (round == -1) ? style.round : round;
		
		if (border) DrawingMethods.roundedRect(mc, false, 0, 0, width, height, r.toString(), [style.borderColor], [style.backgroundAlpha], [1]);
		if (style.background && !style.bgGradient) DrawingMethods.roundedRect(mc, false, b, b, width - b * 2, height - b * 2, Math.max(0, r - b).toString(), [style.backgroundColor], [style.backgroundAlpha], [1]);
		else if (style.background && style.bgGradient) {
			var m:Object = null;
			if (style.bgGradientHeight > 0) {
				m = {};
				m = { matrixType:"box", x: b, y: b, w: width - b * 2, h: style.bgGradientHeight, r: 90 * (Math.PI / 180) };
			}
			DrawingMethods.roundedRect(mc, false, b, b, width - b * 2, height - b * 2, Math.max(0, r - b).toString(), style.bgGradientColors, [style.backgroundAlpha], style.bgGradientRatios, m);
		}
		
	}
    
}
